package com.dev.dream.exochatlockscreen.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dev.dream.exochatlockscreen.LockScreenAppActivity;
import com.dev.dream.exochatlockscreen.R;
import com.dev.dream.exochatlockscreen.model.NotifiItem;

public class NotifiAdapter extends BaseAdapter {

	LockScreenAppActivity activity;
	private Context context;
	private ArrayList<NotifiItem> smsItems;

	public NotifiAdapter(Context context, ArrayList<NotifiItem> appToolbar,
			LockScreenAppActivity activity) {
		this.context = context;
		this.smsItems = appToolbar;
		this.activity = activity;

	}

	@Override
	public int getCount() {
		return smsItems.size();
	}

	@Override
	public Object getItem(int position) {
		return smsItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private class ViewHolder {

		TextView txtDate;
		TextView txtName;
		TextView txtBody;
		LinearLayout llSms;
		ImageView img_phone;
		ImageView img_sms;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater) context
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.item_sms, null);

			holder = new ViewHolder();

			holder.txtDate = (TextView) convertView
					.findViewById(R.id.tv_sms_date);
			holder.txtName = (TextView) convertView
					.findViewById(R.id.tv_sms_name);
			holder.txtBody = (TextView) convertView
					.findViewById(R.id.tv_sms_body);
			holder.llSms = (LinearLayout) convertView
					.findViewById(R.id.llSmsItem);
			holder.img_phone = (ImageView) convertView
					.findViewById(R.id.img_phone_icon);
			holder.img_sms = (ImageView) convertView
					.findViewById(R.id.img_sms_icon);

			convertView.setTag(holder);

			holder.llSms.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					NotifiItem info = (NotifiItem) v.getTag();
					if (info.getType() == NotifiItem.SMS) {
						Intent defineIntent = new Intent(Intent.ACTION_VIEW);
						defineIntent.setData(Uri
								.parse("content://mms-sms/conversations/"
										+ info.getThread_id()));
						activity.startActivity(defineIntent);
					} else {
						Intent intentCall = new Intent(Intent.ACTION_VIEW, Uri.parse("content://call_log/calls"));  // Intent(Intent.ACTION_VIEW,ContactsContract.Contacts.CONTENT_URI);
						activity.startActivity(intentCall);
					}
					// Toast.makeText(context, info.getId() + "", 1000).show();
				}
			});

		} else {
			holder = (ViewHolder) convertView.getTag();

		}

		// convertView.setClickable(true);

		// imgIcon.setImageResource(appAdsItem.get(position).getIcon());
		NotifiItem info = smsItems.get(position);
		holder.txtDate.setText(info.getDate());
		holder.txtName.setText(info.getName());
		holder.txtBody.setText(info.getBody());
		holder.llSms.setTag(info);
		if (info.getType() == NotifiItem.SMS) {
			holder.img_phone.setVisibility(View.GONE);
		} else {
			holder.img_sms.setVisibility(View.GONE);
		}
		// holder.txtTitle.setSelected(true);

		// Log.d("Kai", appToolbar.get(position).getIcon() +"");

		// System.out.println(appAdsItem.get(position).getRate() +"");

		return convertView;
	}

}
